package com.ruoyi.web.controller.system;

import java.io.File;
import java.util.List;

import com.ruoyi.common.config.Global;
import com.ruoyi.common.config.ServerConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Appendix;
import com.ruoyi.system.service.IAppendixService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 附件Controller
 *
 * @author ruoyi
 * @date 2020-04-13
 */
@Controller
@RequestMapping("/system/appendix")
public class AppendixController extends BaseController {
    private String prefix = "system/appendix";

    @Autowired
    private IAppendixService appendixService;

    @Autowired
    private ServerConfig serverConfig;

    @RequiresPermissions("system:appendix:view")
    @GetMapping("/{id}/{docType}")
    public String appendix(@PathVariable Long id, @PathVariable Integer docType,ModelMap mmap) {
        mmap.put("docId", id);
        mmap.put("docType", docType);
        return prefix + "/appendix";
    }

    /**
     * 查询附件列表
     */
    @RequiresPermissions("system:appendix:list")
    @PostMapping("/list/{id}/{docType}")
    @ResponseBody
    public TableDataInfo list(Appendix appendix,@PathVariable Long id, @PathVariable Integer docType) {
        startPage();
        appendix.setDocId(id);
        appendix.setDocType(docType);
        List<Appendix> list = appendixService.selectAppendixList(appendix);
        return getDataTable(list);
    }



    /**
     * 通用上传请求
     */
    @PostMapping("/upload")
    @ResponseBody
    public AjaxResult uploadFile(MultipartFile file, Long docId, Integer docType) throws Exception {
        try {
            System.out.println(docId);
            System.out.println(docType);
            String originalFilename = file.getOriginalFilename();
            // 上传文件路径
            String filePath = Global.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            ajax.put("fileName", fileName);
            ajax.put("url", url);

            Appendix appendix = new Appendix();
            appendix.setAppendixLocation(fileName);
            appendix.setAppendixName(originalFilename);
            appendix.setDocId(docId);
            appendix.setDocType(docType);
            appendixService.insertAppendix(appendix);
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }



    /**
     * 新增附件
     */
    @GetMapping("/add/{id}/{docType}")
    public String add(@PathVariable Long id, @PathVariable Integer docType,ModelMap mmap) {
        mmap.put("docId", id);
        mmap.put("docType", docType);
        return prefix + "/add";
    }



    /**
     * 删除附件
     */
    @RequiresPermissions("system:appendix:remove")
    @Log(title = "附件", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        String[] idArr = ids.split(",");
        for (String id:idArr){
            long aid = Long.parseLong(id);
            Appendix appendix = appendixService.selectAppendixById(aid);
            String appendixLocation = appendix.getAppendixLocation();
            String localPath = Global.getProfile();
            // 数据库资源地址
            String downloadPath = localPath + appendixLocation;
            File file = new File(downloadPath);
            if (file.exists()){
                file.delete();
            }
        }
        return toAjax(appendixService.deleteAppendixByIds(ids));
    }
}
